package com.bankcomm.ui.specialwidget;

import java.util.ArrayList;

import com.bankcomm.common.ConfigManager;
import com.rytong.tools.ui.Component;
import com.rytong.tools.ui.LPAlertDialog;
import com.rytong.tools.ui.LPRadio;
import com.rytong.tools.ui.LPSelect;
import com.rytong.tools.utils.Utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Paint.Style;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 该界面为地址查询界面，主要分为两部分：上部为查询控件，下部为列表控件，显示查询出的内容
 * 
 * @author Boston
 * 
 */
public class BOCSearchView {
    // 该界面的控件集合
    ArrayList<Component> components_;
    // 界面上部查询控件套件
    public LPAutoCompleteTextView autoText_;

    ListView listView_;
    ArrayAdapter arrayAdapter_;
    String[] text_;

    searchLinearLayout ll_;

    LinearLayout llr_;

    ScrollView sc_;

    // 改变量主要用于取消警告框
    public LPAlertDialog ad_;

    public static BOCSearchView BOCSV;

    public static LPSelect LPS;

    static final class searchLinearLayout extends LinearLayout {

        public searchLinearLayout(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            this.setBackgroundColor(0xFF000000);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            Shader shader = new LinearGradient(0, 0, 0, this.getHeight(), new int[] {
                    BOCSearchView.BOCSV.LPS.bgColor(), Color.WHITE }, null, Shader.TileMode.CLAMP);
            Paint paint = new Paint();
            paint.setShader(shader);
            paint.setStyle(Style.FILL);
            canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), paint);
            super.onDraw(canvas);
        }
    }

    public BOCSearchView() {
        // TODO Auto-generated constructor stub

    }

    public static BOCSearchView getInstance(Context arg0, ArrayList<Component> views) {
        if (null == BOCSV) {
            BOCSV = new BOCSearchView();
        }
        BOCSV.init(arg0, views);
        return BOCSV;
    }

    private void init(Context arg0, ArrayList<Component> components) {
        final String awokeText = "请输入城市头一个字母,例如要查询北京,请输入:bj";
        // 赋空ad_和adb_，否则在弹出界面按BACK键再点击下拉框会出现弹出框无内容情况
        ad_ = null;
        components_ = components;
        // 初始化垂直滚动域
        if (null == sc_)
            sc_ = new ScrollView(arg0);
        // 弹出框的总管理器
        if (null == ll_)
            ll_ = new searchLinearLayout(arg0);
        // 设置管理器纵向排列
        ll_.setOrientation(LinearLayout.VERTICAL);
        if (null == llr_)
            llr_ = new LinearLayout(arg0);
        // 设置管理器纵向排列
        llr_.setOrientation(LinearLayout.VERTICAL);

        sc_.removeAllViews();
        ll_.removeAllViews();
        llr_.removeAllViews();
        TextView tv = new TextView(arg0);
        tv.setText(awokeText);
        tv.setTextColor(BOCSV.LPS.fgColor());
        ll_.addView(tv);
        Component cmp;
        for (int i = 0; i < components.size(); i++) {
            try {
                cmp = components.get(i);
            } catch (Exception e) {
                cmp = null;
            }
            if (cmp == null) {
                continue;
            }
            if (cmp instanceof LPAutoCompleteTextView) {
                // 将自动联想输入控件添加至弹出框总控件管理器
                ll_.addView(cmp.getLPView());
            } else {
                // 将非输入控件添加至垂直管理器，用于添加至滑动域用
                if (cmp instanceof LPRadio) {
                    LPRadio lpr = (LPRadio) cmp;
                    llr_.addView(lpr.getLPView(), -1);
                }
            }
        }
        // 将非包含输入控件的管理器添加至滑动域，方便滑动查看浏览
        sc_.addView(llr_);
        // 弹出框总管理器添加滑动域
        ll_.addView(sc_);

        ViewGroup vg = (ViewGroup) ll_.getParent();
        // 由于引用的ll_管理器唯一，如果此处不将管理器清空，Alert又重新设置该管理器的时候会报管理器已设置（添加）异常
        if (null != vg)
            vg.removeAllViews();

        if (null == ad_){
            int alertdialog_style = Utils.getResourcesId(ConfigManager.currentView_, "alertdialog", "style");
            ad_ = new LPAlertDialog(arg0,alertdialog_style,ll_);
        }

        ad_.setContentView(ll_);
        ad_.show();
    }

    public static void doSearch(Context context, String text) {
        // 根据自动输入联想控件中获得的内容执行搜索操作
        LPRadio lpr;
        // 清空可选按钮控件管理器
        if ((null == BOCSV) || (null == BOCSV.llr_))
            return;
        BOCSV.llr_.removeAllViews();
        Component cmpR;
        for (int j = 0; j < BOCSV.components_.size(); j++) {
            try {
                cmpR = BOCSV.components_.get(j);
            } catch (Exception e) {
                continue;
            }
            if (cmpR instanceof LPRadio) {
                lpr = (LPRadio) cmpR;
                String searchKey = lpr.searchkey();
                if ((null != searchKey) && (searchKey.indexOf(text) != -1)) {
                    // 可搜索单选按钮控件中包含用户输入的字符串
                    BOCSV.llr_.addView(lpr.getLPView(), -1);
                    BOCSV.llr_.invalidate();
                }
            }
        }
        // 如果没有查询到任何匹配信息，则在管理器中加入10个空域，不至于使弹出警告框里面就一个自动输入联想控件
        if (BOCSV.llr_.getChildCount() == 0) {
            for (int i = 0; i < 10; i++) {
                BOCSV.llr_.addView(new TextView(context));
            }
            BOCSV.llr_.invalidate();
        }
    }

    public void setSelectView(LPSelect lpSelect) {
        // TODO Auto-generated method stub
        LPS = lpSelect;
    }

}
